//动态导入json数据
	
var xmlRequest = createXHQ();

xmlRequest.open("GET","../json/shopping_cart.json");
xmlRequest.send();

xmlRequest.onreadystatechange=function(){
	
	if(xmlRequest.readyState===4){
		if(xmlRequest.status===200){
			var result1 = xmlRequest.responseText;
			var resultObject = JSON.parse(result1);	
			
			userid = resultObject.userId;
			shopid = resultObject.shop_id;
			
			var userLogin = document.getElementById("user");
			if(resultObject.userName!=null&&resultObject.userName!=""){
				userLogin.innerHTML=resultObject.userName;
				userLogin.href="../user_more.html";
			}else{
				window.location.href="../login.html";
			}
			
			var f_div = document.getElementById("main-nav");
			
			for(var i=0;i<resultObject.userShop.length;i++){
				
				var box1 = document.createElement("div");
				box1.className="nav-s1";
				
				var text1 = "<div class='nav-s1'>"+
								"<div class='nav-s1-ch'>"+
									"<input type='checkbox' name='shopID' />"+
								"</div>"+
								
								"<div class='nav-s1-image'>"+
									"<img src="+ resultObject.userShop[i].shopImg +" />"+
								"</div>"+
								"<div class='nav-s1-text'>"+
									"<span>"+ resultObject.userShop[i].shopTitle +"</span>"+
								"</div>"+
								"<div class='nav-s1-monay'>"+
									"<span>￥<em>"+ resultObject.userShop[i].originalMoney +"</em></span>"+
								"</div>"+
								"<div class='nav-s1-munber'>"+
									"<span>"+
										"<input type='number' class='nums' value="+ resultObject.userShop[i].shopNumber  +"  />"+
									"</span>"+
								"</div>"+
								"<div class='nav-s1-monays'>"+
									"<span>￥<em class='mon'>"+ resultObject.userShop[i].currentMoney  +"</em></span>"+
								"</div>"+
								"<div class='nav-s1-caozuo'>"+
									"<span>"+
										"<a href="+ "#?shopCradId="+ resultObject.userShop[i].shopCardId  +">删除</a>"+
									"</span>"+
								"</div>"+
							"</div>";
				
				box1.innerHTML=text1;
				f_div.appendChild(box1);
			}		
			
		}else{
			alert("错误信息："+xmlRequest.status);
		}
	}
}



window.onload=function(){
	
	sum();
	
	//商品个数错误纠正
	var num1 = document.getElementsByClassName("nums"); 
	
	for(var i=0;i<num1.length;i++){
		
		num1[i].onblur=function(){

			var num = this.value;
			
			if(num<=1){
				this.value = 1;
			}else{
				this.value = Math.round(num);
			}
			
			sum();
		}
	
	}
	//点击全选进行全选计算
	document.getElementsByName("all")[0].onclick=function(){
		
		var allChecked = document.getElementsByName("all")[0];
		if(allChecked.checked == true){
			
			var selectChecked = document.getElementsByName("shopID");
			
			for(var i=0;i<selectChecked.length;i++){
				
				selectChecked[i].checked = true;
				
			}
			
		}else{
			
			var selectChecked = document.getElementsByName("shopID");
			
			for(var i=0;i<selectChecked.length;i++){
				
				selectChecked[i].checked = false;
				
			}
			
		}
		
		sum();
		
	}
	
	//点击单个进行计算是否为全选
	var selectChecked = document.getElementsByName("shopID");
	for(var i=0;i<selectChecked.length;i++){
		
		selectChecked[i].onclick=function(){
			
			if(varfiyAll()){
				document.getElementsByName("all")[0].checked = true;
			}else{
				document.getElementsByName("all")[0].checked = false;
			}
			
			sum();
			
		}
		
	}
	
	
	
	
	
	
}

//计算总价格
function sum(){
	
	var sumMonay = 0;
	
	var s = 0;
	
	var nums = document.getElementsByClassName("nums");
	
	for(var i=0;i<nums.length;i++){
		if(document.getElementsByName("shopID")[i].checked == true){
			s ++;
			var monay =document.getElementsByClassName("mon")[i].innerHTML;
			
			var num = document.getElementsByClassName("nums")[i].value;
			
			sumMonay += monay*num;
			
			
		}
	}
	
	document.getElementById("sumMonay").innerHTML=sumMonay;
	document.getElementById("ss").innerHTML=s;
	
}

//判断是否全选
function varfiyAll(){
	
	var result1 = true;
	var selectChecked = document.getElementsByName("shopID");
	for(var i=0;i<selectChecked.length;i++){
		
		if(selectChecked[i].checked != true){
			result1 = false;
		}
		
	}
	
	return result1;
	
}